﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization; 
 

namespace WindowsPhoneHackathonFall2011
{  

 
    public class DataSaver<PointTime[]> 
    { 
        private const string TargetFolderName = "bounceData"; 
        private DataContractSerializer _mySerializer; 
        private IsolatedStorageFile _isoFile; 
        IsolatedStorageFile IsoFile 
        { 
            get 
            { 
                if (_isoFile == null) 
                    _isoFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 
                return _isoFile; 
            } 
        } 
 
        public DataSaver() 
        { 
            _mySerializer = new DataContractSerializer(typeof(PointTime[])); 
        } 
 
        public void SaveMyData(PointTime[] sourceData, String targetFileName) 
        { 
            string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, targetFileName); 
            if (!IsoFile.DirectoryExists(TargetFolderName)) 
                IsoFile.CreateDirectory(TargetFolderName); 
            try 
            { 
                using (var targetFile = IsoFile.CreateFile(TargetFileName)) 
                { 
                    _mySerializer.WriteObject(targetFile, sourceData); 
                } 
            } 
            catch (Exception e) 
            { 
                IsoFile.DeleteFile(TargetFileName); 
            }  
 
        } 
 
        public PointTime[] LoadMyData(string sourceName) 
        { 
            PointTime[] retVal = default(PointTime[]); 
            string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, sourceName); 
            if (IsoFile.FileExists(TargetFileName)) 
                using (var sourceStream = IsoFile.OpenFile(TargetFileName, FileMode.Open)) 
                { 
                    retVal = (PointTime[])_mySerializer.ReadObject(sourceStream); 
                } 
            return retVal; 
        } 
    } 
}
